package com.meirengu.channel.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.meirengu.channel.service.YiCaiDaoService;
import com.meirengu.utils.HttpUtil;
import com.meirengu.utils.MD5Util;
import com.meirengu.utils.StringUtil;
import com.meirengu.webview.common.Constants;
import com.meirengu.webview.model.KwYcdPush;
import com.meirengu.webview.model.KwYcdPushLog;
import com.meirengu.webview.service.KwYcdPushLogService;
import com.meirengu.webview.service.KwYcdPushService;
import com.meirengu.webview.utils.ConfigUtil;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 壹财道贷款接口
 *
 * @author Mao
 * @create 2017-12-15 12:11
 */
@Service
public class YiCaiDaoServiceImpl implements YiCaiDaoService {

    Logger logger = LoggerFactory.getLogger(YiCaiDaoServiceImpl.class);

    @Autowired
    private KwYcdPushService kwYcdPushService;
    @Autowired
    private KwYcdPushLogService kwYcdPushLogService;


    /**
     * 中腾信
     *
     * @param ip
     * @param name
     * @param sex
     * @param phone
     * @param birthday
     * @param city
     * @param loanAmount
     * @param house
     * @param car
     * @param xyCard
     * @param payway
     * @param hire
     * @param wage
     * @param shebaoTime
     * @param epfTime
     * @param idCard
     * @param shouxian
     * @return
     */
    @Override
    public Integer reg(String ip, String name, String sex, String phone, String birthday,
                       String city, String loanAmount, String house, String car, String xyCard, String payway,
                       String hire, String wage, String shebaoTime, String epfTime, String idCard, String shouxian) {

        StringBuffer url = new StringBuffer(ConfigUtil.getConfig("yicaidao.url"));
        String cId = ConfigUtil.getConfig("yicaidao.cId");
        String key = ConfigUtil.getConfig("yicaidao.key");
        url.append("?c=").append(cId).append("&t=").append("load").append("&b=mobile").append("&v=").append(MD5Util.get32MD5(birthday + phone + cId + key));
        Map<String, String> params = new HashMap<>();
        params.put("Name", name);
        //格式：1981-01
        params.put("Birthday", birthday);
        params.put("Sex", sex);
        params.put("Phone", phone);
        params.put("IP", ip);
        params.put("City", city);
        params.put("LoanAmount", loanAmount);
        params.put("Houes", house);
        params.put("Car", car);
        params.put("XyCard", xyCard);
        params.put("Payway", payway);
        params.put("Hire", hire);
        params.put("Wage", wage);
        params.put("SheBaoTime", shebaoTime);
        params.put("EPFTime", epfTime);
        if (!StringUtil.isEmpty(idCard)) {
            params.put("IDCard", idCard);
        }
        if (!StringUtil.isEmpty(shouxian)) {
            params.put("Shouxian", shouxian);
        }

        //接口调用信息表
        KwYcdPush kwYcdPush = new KwYcdPush();
        kwYcdPush.setInterfaceCode("ycdctc");
        kwYcdPush.setInterfaceName("壹财道中腾信");
        kwYcdPush.setMobile(phone);
        kwYcdPush.setCode(Constants.BUSINESS_STATUS_FAIL);


        KwYcdPushLog kwYcdPushLog = new KwYcdPushLog();
        kwYcdPushLog.setInterfaceCode("ycdctc");
        kwYcdPushLog.setInterfaceName("壹财道中腾信");
        kwYcdPushLog.setMobile(phone);
        kwYcdPushLog.setSendUrl(url.toString());
        kwYcdPushLog.setParams(params.toString());


        try {
            HttpUtil.HttpResult hr = HttpUtil.doPostForm(url.toString(), params);
            kwYcdPushLog.setBackUrl(hr.toString());
            kwYcdPushLog.setHttpCode(hr.getStatusCode());
            kwYcdPushLog.setHttpContent(hr.getContent());
            int code = hr.getStatusCode();
            if (code == 200) {
                logger.info("==YiCaiDaoServiceImpl.reg http requesst success. result:{}", hr.toString());
                String content = hr.getContent();
                JSONObject returnMsg = JSONObject.parseObject(content);
                String status = returnMsg.getString("success");
                if ("true".equals(status)) {
                    kwYcdPush.setCode(Constants.BUSINESS_STATUS_SUCCESS);
                    kwYcdPush.setMessage("成功");
                    logger.info("==YiCaiDaoServiceImpl.reg add success");
                } else {
                    String errCode = returnMsg.getString("errCode");
                    String errMsg = returnMsg.getString("errMsg");
                    if ("6".equals(errCode)) {
                        kwYcdPush.setCode(Constants.BUSINESS_STATUS_REPEAT);
                    } else {
                        kwYcdPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                    }
                    kwYcdPush.setMessage(errCode + ":" + errMsg);
                    logger.warn("==YiCaiDaoServiceImpl.reg add fail");
                }
            } else {
                kwYcdPushLog.setBackUrl(hr.toString());
                kwYcdPushLog.setHttpCode(code);
                kwYcdPushLog.setHttpContent(hr.toString());
                logger.warn("==YiCaiDaoServiceImpl.reg http requesst fail. code:{}, result:{}", code, hr.toString());
            }

        } catch (Exception e) {
            kwYcdPushLog.setHttpCode(408);
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        } finally {
            kwYcdPushService.insert(kwYcdPush);
            kwYcdPushLog.setPushId(kwYcdPush.getId());
            kwYcdPushLogService.insert(kwYcdPushLog);
            return kwYcdPush.getCode();
        }
    }


    /**
     * 普惠
     *
     * @param ip
     * @param name
     * @param sex
     * @param phone
     * @param birthday
     * @param city
     * @param loanAmount
     * @param house
     * @param car
     * @param xyCard
     * @param payway
     * @param hire
     * @param wage
     * @param shebaoTime
     * @param epfTime
     * @param idCard
     * @param shouxian
     * @return
     */
    @Override
    public Integer regPuhui(String ip, String name, String sex, String phone, String birthday,
                            String city, String loanAmount, String house, String car, String xyCard, String payway,
                            String hire, String wage, String shebaoTime, String epfTime, String idCard, String shouxian) {

        StringBuffer url = new StringBuffer(ConfigUtil.getConfig("yicaidao.url"));
        String cId = ConfigUtil.getConfig("yicaidao.puhui.cId");
        String key = ConfigUtil.getConfig("yicaidao.key");
        url.append("?c=").append(cId).append("&t=").append("load").append("&b=mobile").append("&v=").append(MD5Util.get32MD5(birthday + phone + cId + key));
        Map<String, String> params = new HashMap<>();
        params.put("Name", name);
        //格式：1981-01
        params.put("Birthday", birthday);
        params.put("Sex", sex);
        params.put("Phone", phone);
        params.put("IP", ip);
        params.put("City", city);
        params.put("LoanAmount", loanAmount);
        params.put("Houes", house);
        params.put("Car", car);
        params.put("XyCard", xyCard);
        params.put("Payway", payway);
        params.put("Hire", hire);
        params.put("Wage", wage);
        params.put("SheBaoTime", shebaoTime);
        params.put("EPFTime", epfTime);
        if (!StringUtil.isEmpty(idCard)) {
            params.put("IDCard", idCard);
        }
        if (!StringUtil.isEmpty(shouxian)) {
            params.put("Shouxian", shouxian);
        }

        //接口调用信息表
        KwYcdPush kwYcdPush = new KwYcdPush();
        kwYcdPush.setInterfaceCode("ycdph");
        kwYcdPush.setInterfaceName("壹财道普惠");
        kwYcdPush.setMobile(phone);
        kwYcdPush.setCode(Constants.BUSINESS_STATUS_FAIL);


        KwYcdPushLog kwYcdPushLog = new KwYcdPushLog();
        kwYcdPushLog.setInterfaceCode("ycdph");
        kwYcdPushLog.setInterfaceName("壹财道普惠");
        kwYcdPushLog.setMobile(phone);
        kwYcdPushLog.setSendUrl(url.toString());
        kwYcdPushLog.setParams(params.toString());


        try {
            HttpUtil.HttpResult hr = HttpUtil.doPostForm(url.toString(), params);
            kwYcdPushLog.setBackUrl(hr.toString());
            kwYcdPushLog.setHttpCode(hr.getStatusCode());
            kwYcdPushLog.setHttpContent(hr.getContent());
            int code = hr.getStatusCode();
            if (code == 200) {
                logger.info("==YiCaiDaopServiceImpl.regpuhui http requesst success. result:{}", hr.toString());
                String content = hr.getContent();
                JSONObject returnMsg = JSONObject.parseObject(content);
                String status = returnMsg.getString("success");
                if ("true".equals(status)) {
                    kwYcdPush.setCode(Constants.BUSINESS_STATUS_SUCCESS);
                    kwYcdPush.setMessage("成功");
                    logger.info("==YiCaiDaoServiceImpl.regpuhui add success");
                } else {
                    String errCode = returnMsg.getString("errCode");
                    String errMsg = returnMsg.getString("errMsg");
                    if ("6".equals(errCode)) {
                        kwYcdPush.setCode(Constants.BUSINESS_STATUS_REPEAT);
                    } else {
                        kwYcdPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                    }
                    kwYcdPush.setMessage(errCode + ":" + errMsg);
                    logger.warn("==YiCaiDaoServiceImpl.regpuhui add fail");
                }
            } else {
                kwYcdPushLog.setBackUrl(hr.toString());
                kwYcdPushLog.setHttpCode(code);
                kwYcdPushLog.setHttpContent(hr.toString());
                logger.warn("==YiCaiDaoServiceImpl.regpuhui http requesst fail. code:{}, result:{}", code, hr.toString());
            }

        } catch (Exception e) {
            kwYcdPushLog.setHttpCode(408);
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        } finally {
            kwYcdPushService.insert(kwYcdPush);
            kwYcdPushLog.setPushId(kwYcdPush.getId());
            kwYcdPushLogService.insert(kwYcdPushLog);
            return kwYcdPush.getCode();
        }
    }

}
